//
//  PageScroView.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/3/26.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "PageScroView.h"

@interface PageScroView ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView * scroView;
@property(nonatomic,strong)UIView * slectedView;
@end


@implementation PageScroView

- (instancetype)initWithFrame:(CGRect)frame Titles:(NSArray *)titles complect:(void(^)(NSInteger index))complect{
    self = [super initWithFrame:frame];
    if (self) {
        _scroView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _scroView.showsHorizontalScrollIndicator = NO;
        _scroView.delegate = self;
//        _scroView.pagingEnabled = YES;
        _scroView.contentSize = flexibleSize(CGSizeMake(titles.count*97, 0), YES);
        [self addSubview:_scroView];
        for (int i = 0; i<titles.count; i++) {

            
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = flexibleFrame(CGRectMake(97*i, 0, 97, frame.size.height), YES);
            [button setTitleColor:[UIColor colorWithWhite:0.365 alpha:1.000] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:13*VerticalTexeRatio()];
            [button setTitle:titles[i] forState:UIControlStateNormal];
            button.tag = 100+i;
            [button addTarget:self action:@selector(scroHandel:) forControlEvents:UIControlEventTouchUpInside];
            [_scroView addSubview:button];
            

        }
        
        UIView * line = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 34, 97*titles.count, 1), YES)];
        line.backgroundColor = [UIColor colorWithWhite:0.882 alpha:1.000];
        [_scroView addSubview:line];
        
        _slectedView = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 33.5, 97, 1.5), YES)];
        _slectedView.backgroundColor = [UIColor colorWithRed:0.980 green:0.624 blue:0.000 alpha:1.000];
        [_scroView addSubview:_slectedView];
        
        
    }
    return self;
}

- (void)scroHandel:(UIButton *)sender{
    [UIView animateWithDuration:0.1 animations:^{
        _slectedView.frame = flexibleFrame(CGRectMake(97*(sender.tag-100), 33.5, 97, 1.5), YES);
    }];
   [ _scroView scrollRectToVisible:flexibleFrame(CGRectMake(97*(sender.tag -100), 0, _scroView.frame.size.width/VerticalRatio(), _scroView.frame.size.height/VerticalRatio()), YES) animated:YES];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
// NSInteger index=(self.scroView.frame.size.width*0.5+self.scroView.contentOffset.x)/self.scroView.frame.size.width;
//    
//    [self.scroView setContentOffset:CGPointMake(index*self.scroView.frame.size.width, 0) animated:YES];
}

@end
